package com.superhelper.common.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.superhelper.common.domain.AuserShop;
import com.superhelper.common.domain.DeliverAccount;
import com.superhelper.common.domain.DeliverRecord;
import com.superhelper.common.domain.TakeOutOrder;
import com.superhelper.common.domain.TakeOutOrderBackup;
import com.superhelper.common.domain.TakeOutOrderGroup;
import com.superhelper.common.domain.TakeOutOrderGroupItem;
import com.superhelper.common.domain.TakeOutOrderProcess;
import com.superhelper.common.domain.User;

public interface TakeOutOrderMapper {
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into takeout_order(id,auserId,plfOrderId,plfOrderCode,`type`,userId,shopId,shopName,openId,"
            + " daySn,status,refundStatus,plfUserId,plfCreateAt,activeAt,deliverFee,"
            + "deliverTime,description,thirdDeliver,invoiced,invoice,book,payType,"
            + "totalPrice,oriPrice,consigness,phone,deliverLat,deliverLon,deliverPoi,"
            + "income,serviceRate,serviceFee,hongbao,packageFee,activityTotal,shopPart,"
            + "plfPart,downgraded,dist,createAt)values(#{id},#{auserId},#{plfOrderId},#{plfOrderCode},#{type},#{userId},#{shopId},#{shopName},#{openId},"
            + "#{daySn},#{status},#{refundStatus},#{plfUserId},#{plfCreateAt},#{activeAt},#{deliverFee},"
            + "#{deliverTime},#{description},#{thirdDeliver},#{invoiced},#{invoice},#{book},#{payType},"
            + "#{totalPrice},#{oriPrice},#{consigness},#{phone},#{deliverLat},#{deliverLon},#{deliverPoi},"
            + "#{income},#{serviceRate},#{serviceFee},#{hongbao},#{packageFee},#{activityTotal},#{shopPart},"
            + "#{plfPart},#{downgraded},#{dist},#{createAt})")
    int insertOrder(TakeOutOrder order);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into takeout_order_group(id,orderId,name,`type`)values(#{id},#{orderId},#{name},#{type})")
    int insertGroup(TakeOutOrderGroup group);

    @Insert("insert into takeout_order_group_item(id,groupId,orderId,skuId,name,categoryId,price,"
            + "quantity,total,newSpecs,attributes,additions,extendCode,barCode,weight)values(#{id},#{groupId},#{orderId},#{skuId},#{name},#{categoryId},#{price},"
            + "#{quantity},#{total},#{newSpecs},#{attributes},#{additions},#{extendCode},#{barCode},#{weight})")
    int insertGroupItem(TakeOutOrderGroupItem item);

    @Insert("insert into takeout_order_backup values(#{orderId},#{content})")
    int insertOrderBackup(TakeOutOrderBackup order);

    @Select("select * from takeout_order where plfOrderId=#{plfOrderId} and `type`=#{type}")
    TakeOutOrder getByPlfOrderIdAndType(@Param("plfOrderId") String plfOrderId, @Param("type") String type);

    @Update("update takeout_order set lastStatus=status,statusMsg='商家已接单',status = 'valid',validAt=current_timestamp where plfOrderId=#{plfOrderId} and `type`=#{type} and "
            + "status in( 'unprocessed','pending','apply_cancel')")
    void updateAccepted(@Param("plfOrderId") String plfOrderId, @Param("type") String type);

    @Update("update takeout_order set lastStatus=status,statusMsg='已完成',status = 'settled' where plfOrderId=#{plfOrderId} and `type`=#{type} and "
            + " status in('valid','unprocessed','pending','delivering','apply_cancel')")
    void updateSettled(@Param("plfOrderId") String plfOrderId, @Param("type") String type);

    @Update("update takeout_order set lastStatus=status,statusMsg='已完成',status = 'settled' where id=#{value} and "
            + " status in('valid','unprocessed','pending','delivering','apply_cancel')")
    void updateSettledById(Long orderId);

    @Update("update takeout_order set lastStatus=status,statusMsg='配送中',status = 'delivering' where id=#{id} and "
            + " status in('valid','unprocessed','pending')")
    void updateDeliveringById(@Param("id") Long id);

    @Update("update takeout_order set lastStatus=status, status = 'invalid' ,statusMsg=#{statusMsg} where plfOrderId=#{plfOrderId} and `type`=#{type}")
    void updateInvalid(@Param("plfOrderId") String plfOrderId, @Param("type") String type,
            @Param("statusMsg") String statusMsg);

    @Update("update takeout_order set lastStatus=status, status = 'valid' ,statusMsg=#{statusMsg} where plfOrderId=#{plfOrderId} and `type`=#{type}")
    void updateValid(@Param("plfOrderId") String plfOrderId, @Param("type") String type,
            @Param("statusMsg") String statusMsg);

    @Update("update takeout_order set status = lastStatus ,statusMsg=#{statusMsg} where plfOrderId=#{plfOrderId} and `type`=#{type}")
    void updateCallBack(@Param("plfOrderId") String plfOrderId, @Param("type") String type,
            @Param("statusMsg") String statusMsg);

    @Update("update takeout_order set lastStatus=status,status = 'refunding',statusMsg=#{statusMsg} where plfOrderId=#{plfOrderId} and `type`=#{type} and status !='refunding'")
    void updateRefunding(@Param("plfOrderId") String plfOrderId, @Param("type") String type,
            @Param("statusMsg") String statusMsg);

    @Select("select * from auser_shop where shopId=#{shopId} and shop_type=#{type} ")
    AuserShop getShop(@Param("shopId") String shopId, @Param("type") String type);

    @Select("select * from auser_shop where partnerId=#{partnerId} and shop_type=#{type} ")
    AuserShop getShopByPartnerId(@Param("partnerId") String partnerId, @Param("type") String type);

    @Select("select * from deliver_account where auserId=#{auserId} and deliverType=#{deliverType}")
    DeliverAccount getDeliverAccountByType(@Param("auserId") Long auserId, @Param("deliverType") Integer deliverType);

    @Select("select * from auser_shop where id=#{value}")
    AuserShop getShopById(Long shopId);

    @Update("update takeout_order set lastStatus=status,statusMsg=#{statusMsg},status = 'apply_cancel' where plfOrderId=#{plfOrderId} and `type`=#{type} and "
            + "status in('unprocessed','valid','pending','delivering','settled')")
    void updateApplyCancel(@Param("plfOrderId") String plfOrderId, @Param("type") String type,
            @Param("statusMsg") String statusMsg);

    @Select("select * from takeout_order where id = #{value}")
    TakeOutOrder getById(Long orderId);

    @Insert("insert into takeout_order_process(orderId,shopId,deliver_state,deliverAt,print_state,printAt,type) values(#{orderId},"
            + "#{shopId},#{deliver_state},#{deliverAt},#{print_state},#{printAt},#{type})")
    void addProcessInfo(TakeOutOrderProcess toop);

    @Select("select * from takeout_order_process where orderId =#{orderId} and type=#{type}")
    TakeOutOrderProcess getProcessInfo(@Param("orderId") Long orderId, @Param("type") int type);

    @Delete("delete from takeout_order_process where orderId =#{value}")
    int deleteOrderProcess(Long id);

    @Select("select * from deliver_record where orderId=#{value} order by createAt desc limit 1")
    DeliverRecord getDeliverRecordByOrderId(Long id);

    @Select("select ifnull(version,0) from auser_shop where id =#{value}")
    Integer getVersionById(Long auserShopId);

    @Update("update auser_shop set version = version+1 where id =#{id} and version = #{version}")
    int updateVersionById(@Param("id") Long auserShopId, @Param("version") Integer version);

    @Select("select * from takeout_order where createAt>date_sub(curdate(),interval 2 day) and status in ('unprocessed','refunding','apply_cancel') and refundStatus in('noRefund','applied')")
    List<TakeOutOrder> getUnReceiverOrder();

    @Select("select * from takeout_order where createAt < date_sub(curdate(),interval 10 MINUTE) and status ='unprocessed' and `type` = #{type}")
    List<TakeOutOrder> getUnProcessOrder(@Param("type") String type, @Param("minute") int minute);

    @Select("select item.orderId,item.name,item.price,item.quantity,item.total,g.type,g.name as groopName from takeout_order_group_item as item,takeout_order_group as g where g.id=item.groupId and item.orderId=#{orderId} ")
    List<TakeOutOrderGroupItem> getTakeOutOrderGroupItem(@Param("orderId") Long orderId);

    @Select("select packageName as source,expireAt as createAt  from auser where id=#{auserId}")
    User getUserPackageName(@Param("auserId") Integer auserId);
}
